﻿/*  CamHelper (See http://camhelper.codeplex.com/ or https://bitbucket.org/AndiKS/camhelper for more Info)
    Copyright (C) 2011 Andreas Wimmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using SystemWrapper.IO;

namespace CamHelper.Interpreter
{
    public class ParameterFile : IParameterFile
    {
        public string FileName { get; private set; }
        public string FullFileName { get; private set; }
        public ParameterFileType FileType { get; private set; }
        public string PureContent { get; private set; }
        public List<ParameterFileLine> Lines { get; private set; }
        public ParameterCollection Parameters { get; private set; }
        public List<String> Categories { get; private set; }

        #region Constructor

        public ParameterFile(string path, IFileWrap file)
        {
            FullFileName = path;
            PureContent = file.ReadAllText(path, Encoding.Default);
            SplitContent();
        }

        public ParameterFile(string path, string content)
        {
            FullFileName = path;
            PureContent = content;
            SplitContent();
        }

        #endregion

        #region SplitContent

        private void SplitContent()
        {
            FileName = Path.GetFileName(FullFileName);
            FileType = (Path.GetExtension(FullFileName) == ".frm" ? ParameterFileType.ProcessFile : ParameterFileType.ParameterFile);

            Lines = new List<ParameterFileLine>();
            Parameters = new ParameterCollection();
            Categories = new List<string>();
            string[] lines = PureContent.Split(new String[] { Environment.NewLine }, StringSplitOptions.None);
            string category = String.Empty;

            for (int i = 0; i < lines.Length; i++)
            {
                var pfl = new ParameterFileLine(this, i, lines[i]);
                Lines.Add(pfl);

                if (pfl.LineType == ParameterFileLineType.Category)
                    category = pfl.Category;
                else if (pfl.LineType == ParameterFileLineType.KeyValue)
                {
                    pfl.Category = category;

                    var p = new Parameter(pfl);
                    Parameters.Add(p);

                    if (!Categories.Contains(p.Category))
                        Categories.Add(p.Category);
                }
            }
        }

        #endregion

    }
}
